package networkSimulation.visualisation.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

/**
 * Visualisation frame.
 *
 * @author  Tim Caljé
 *          Jeroen De Raedt
 *          Jorick Van Aken
 */
public class VisualUIFrame extends JFrame{

    private JButton next;
    private JButton previous;
    private JButton first;
    private JPanel imagePanel;
    private ArrayList<ImagePanel> imagePanels;
    private int currentIndex;
    private String inputPath;

    public VisualUIFrame(String inputPath){
       imagePanels = new ArrayList<ImagePanel>();
       this.inputPath = inputPath;
       currentIndex = 0;
       createImagePanels();
       initiateControls();
       addListeners();
       setLayout();
       showFrame();
       showImage();
       handleButtonEnability();
    }

    private void createImagePanels(){
        int counter = 0;
        boolean keepOnGoing = true;
        while(keepOnGoing){
            try {                
                if ((new File(inputPath + counter + ".gif")).exists()) {
                    imagePanels.add(new ImagePanel(new ImageIcon(inputPath + counter + ".gif").getImage()));
                } else {
                    keepOnGoing = false;
                }
                counter++;
            } catch (Exception e){
                keepOnGoing = false;
            }
        }
    }

    private void initiateControls(){
        next = new JButton("Next");
        previous = new JButton("Previous");
        first = new JButton("First");
        imagePanel = new JPanel();
    }

    private void addListeners(){
        next.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (currentIndex < imagePanels.size()-1) {
                    currentIndex += 1;
                } else {
                    currentIndex = imagePanels.size()-1;
                }
                showImage();
            }
        });
        previous.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (currentIndex > 0) {
                    currentIndex -= 1;
                } else {
                    currentIndex = 0;
                }
                showImage();
            }
        });
        first.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                currentIndex = 0;
                showImage();
            }
        });
    }

    private void setLayout(){
        Container userPanel = getContentPane();
        userPanel.setLayout(new BorderLayout());

        JPanel buttonPanel = new JPanel(new FlowLayout());
        buttonPanel.add(first);
        buttonPanel.add(previous);
        buttonPanel.add(next);

        imagePanel = new JPanel(new FlowLayout());
        imagePanel.setAlignmentX(CENTER_ALIGNMENT);

        userPanel.add(imagePanel,BorderLayout.CENTER);
        userPanel.add(buttonPanel,BorderLayout.SOUTH);
    }

    private void showFrame() {
       setDefaultCloseOperation(EXIT_ON_CLOSE);
       setLocation(400, 300);
       setSize(1100,650);
       setMinimumSize(this.getSize());
       setMaximumSize(this.getSize());
       setVisible(true);
       repaint();
   }

    private void showImage(){
        clearImage();
        if (imagePanels.size() == 0){
            // show any kind of message ?         
        } else {
            imagePanel.add(imagePanels.get(currentIndex));
            imagePanel.repaint();
        }
        handleButtonEnability();
        repaint();
    }

    private void clearImage(){
        imagePanel.removeAll();
        repaint();
    }

    private void handleButtonEnability(){
        if (currentIndex == 0) {
            first.setEnabled(false);
            previous.setEnabled(false);
            if (currentIndex == imagePanels.size()-1) {
                next.setEnabled(false);
            } else {
                next.setEnabled(true);
            }
        } else {
            first.setEnabled(true);
            previous.setEnabled(true);
            if (currentIndex == imagePanels.size()-1) {
                next.setEnabled(false);
            } else {
                next.setEnabled(true);
            }
        }
    }

}
